package gene.android.accountingpro;

import gene.android.accountingpro.equations.Equation;
import gene.android.accountingpro.finals.EquationNames;
import gene.android.accountingpro.finals.FieldList;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

public class FormGenerator extends Activity {
	
	public static final String CLASS_TAG = FormGenerator.class.getSimpleName();
	
	public static final String FIELD_NAMES_VALUE_MAP = "fieldnamesvaluemap";
	public static final String REQUIRED_EQUATIONS = "required equations";
	
	private Button nextButton;

	private ArrayList<String> selectedEquationNamesList;
	private List<Equation> selectedEquations;
	private ArrayList<String> requiredFields;
	
	private Hashtable<String, Double> fieldNameValueMap;

	private LinearLayout container;

	private double currentAssets = 0;
	private double currentLiabilities = 0;
	private double cash = 0;
	private double short_term_investments = 0;
	private double accounts_receivable = 0;
	private double total_debt = 0;
	private double total_assets = 0;
	private double total_equity = 0;
	private double income_before_taxes;
	private double interest_charges;
	private double net_credit_sales = 0;
	private double average_net_accounts_receivable = 0;
	private double cost_of_goods_sold = 0;
	private double average_inventory = 0;
	private double net_income = 0;
	private double net_sales = 0;
	private double gross_profit = 0;
	private double interest_expense = 0;
	private double average_assets = 0;
	private double preferred_dividends = 0;
	private double average_common_equity = 0;
	private double income_available_to_common = 0;
	private double weighted_average_number_of_common_shares = 0;

	private EditText currentAssetsField;
	private EditText currentLiabilitiesField;
	private EditText cashField;
	private EditText shortTermInvestmentsField;
	private EditText accountsReceivableField;
	private EditText totalDebtField;
	private EditText totalAssetsField;
	private EditText totalEquityField;
	private EditText incomeBeforeTaxesField;
	private EditText interestChargesField;
	private EditText netCreditSalesField;
	private EditText averageNetAccountsReceivablesField;
	private EditText costOfGoodsSoldField;
	private EditText averageInventoryField;
	private EditText netIncomeField;
	private EditText netSalesField;
	private EditText grossProfitField;
	private EditText interestExpenseField;
	private EditText averageAssetsField;
	private EditText preferredDividendsField;
	private EditText averageCommonEquityField;
	private EditText incomeAvailableToCommonField;
	private EditText weightedAverageNumberOfCommonSharesField;
	


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.formgenerator);

		container = (LinearLayout)findViewById(R.id.container);

		selectedEquationNamesList = getIntent().getStringArrayListExtra(EquationList.SELECTED_EQUATION_ARRAY);
		selectedEquations = EquationNames.equationNamesListToEquationsList(selectedEquationNamesList);

		requiredFields = new ArrayList<String>(getRequiredFieldsList( selectedEquations ));
		createFields();
		
		nextButton = (Button)findViewById(R.id.formgeneratornextbutton);
		nextButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) 
			{
				assignAllValues();
				fieldNameValueMap = new Hashtable<String, Double>(createFieldNameValueMap());
				
				Intent intent = new Intent( FormGenerator.this, ShowSolvedEquations.class );
				intent.putExtra(FIELD_NAMES_VALUE_MAP, fieldNameValueMap);
				intent.putExtra(REQUIRED_EQUATIONS, selectedEquationNamesList);
				startActivity(intent);

			}
		});
	}


	public void assignAllValues()
	{
		if( currentAssetsField != null )
		{
			try
			{
				currentAssets = Double.valueOf(currentAssetsField.getText().toString());
			}
			catch( NumberFormatException e)
			{
				e.printStackTrace();
			}
		}
		if( currentLiabilitiesField != null )
		{
			try
			{
				currentLiabilities = Double.valueOf(currentLiabilitiesField.getText().toString());
			}
			catch( NumberFormatException e)
			{
				e.printStackTrace();
			}
		}
		if( cashField != null )
		{
			try
			{
				cash = Double.valueOf(cashField.getText().toString());
			}
			catch( NumberFormatException e)
			{
				e.printStackTrace();
			}
		}
		if( shortTermInvestmentsField != null )
		{
			try
			{
				short_term_investments = Double.valueOf(shortTermInvestmentsField.getText().toString());
			}
			catch( NumberFormatException e)
			{
				e.printStackTrace();
			}
		}
		if( accountsReceivableField != null )
		{
			try
			{
				accounts_receivable = Double.valueOf(accountsReceivableField.getText().toString());
			}
			catch( NumberFormatException e)
			{
				e.printStackTrace();
			}
		}
		if( totalDebtField != null )
		{
			try
			{
				total_debt = Double.valueOf(totalDebtField.getText().toString());
			}
			catch( NumberFormatException e)
			{
				e.printStackTrace();
			}
		}
		if( totalAssetsField != null )
		{
			try
			{
				total_assets = Double.valueOf(totalAssetsField.getText().toString());
			}
			catch( NumberFormatException e)
			{
				e.printStackTrace();
			}
		}
		if( totalEquityField != null )
		{
			try
			{
				total_equity = Double.valueOf(totalEquityField.getText().toString());
			}
			catch( NumberFormatException e)
			{
				e.printStackTrace();
			}
		}
		if( incomeBeforeTaxesField != null )
		{
			try
			{
				income_before_taxes = Double.valueOf(incomeBeforeTaxesField.getText().toString());
			}
			catch( NumberFormatException e)
			{
				e.printStackTrace();
			}
		}
		if( interestChargesField != null )
		{
			try
			{
				interest_charges = Double.valueOf(interestChargesField.getText().toString());
			}
			catch( NumberFormatException e)
			{
				e.printStackTrace();
			}
		}
		if( netCreditSalesField != null )
		{
			try
			{
				net_credit_sales = Double.valueOf(netCreditSalesField.getText().toString());
			}
			catch( NumberFormatException e)
			{
				e.printStackTrace();
			}
		}
		if( averageNetAccountsReceivablesField != null )
		{
			try
			{
				average_net_accounts_receivable = Double.valueOf(averageNetAccountsReceivablesField.getText().toString());
			}
			catch( NumberFormatException e)
			{
				e.printStackTrace();
			}
		}
		if( costOfGoodsSoldField != null )
		{
			try
			{
				cost_of_goods_sold = Double.valueOf(costOfGoodsSoldField.getText().toString());
			}
			catch( NumberFormatException e)
			{
				e.printStackTrace();
			}
		}
		if( averageInventoryField != null )
		{
			try
			{
				average_inventory = Double.valueOf(averageInventoryField.getText().toString());
			}
			catch( NumberFormatException e)
			{
				e.printStackTrace();
			}
		}
		if( netIncomeField != null )
		{
			try
			{
				net_income = Double.valueOf(netIncomeField.getText().toString());
			}
			catch( NumberFormatException e)
			{
				e.printStackTrace();
			}
		}
		if( netSalesField != null )
		{
			try
			{
				net_sales = Double.valueOf(netSalesField.getText().toString());
			}
			catch( NumberFormatException e)
			{
				e.printStackTrace();
			}
		}
		if( grossProfitField != null )
		{
			try
			{
				gross_profit = Double.valueOf(grossProfitField.getText().toString());
			}
			catch( NumberFormatException e)
			{
				e.printStackTrace();
			}
		}
		if( interestExpenseField != null )
		{
			try
			{
				interest_expense = Double.valueOf(interestExpenseField.getText().toString());
			}
			catch( NumberFormatException e)
			{
				e.printStackTrace();
			}
		}
		if( averageAssetsField != null )
		{
			try
			{
				average_assets = Double.valueOf(averageAssetsField.getText().toString());
			}
			catch( NumberFormatException e)
			{
				e.printStackTrace();
			}
		}
		if( averageCommonEquityField != null )
		{
			try
			{
				average_common_equity = Double.valueOf(averageCommonEquityField.getText().toString());
			}
			catch( NumberFormatException e)
			{
				e.printStackTrace();
			}
		}
		if( preferredDividendsField != null )
		{
			try
			{
				preferred_dividends = Double.valueOf(preferredDividendsField.getText().toString());
			}
			catch( NumberFormatException e)
			{
				e.printStackTrace();
			}
		}
		if( incomeAvailableToCommonField != null )
		{
			try
			{
				income_available_to_common = Double.valueOf(incomeAvailableToCommonField.getText().toString());
			}
			catch( NumberFormatException e)
			{
				e.printStackTrace();
			}
		}
		if( weightedAverageNumberOfCommonSharesField != null )
		{
			try
			{
				weighted_average_number_of_common_shares = Double.valueOf(weightedAverageNumberOfCommonSharesField.getText().toString());
			}
			catch( NumberFormatException e)
			{
				e.printStackTrace();
			}
		}
		
	}

	public void createFields()
	{
		if (requiredFields.contains( FieldList.CURRENT_ASSETS ))
		{
			currentAssetsField = addNewEditTextWithLabel( FieldList.CURRENT_ASSETS );
		}
		if (requiredFields.contains( FieldList.CURRENT_LIABILITES ))
		{
			currentLiabilitiesField = addNewEditTextWithLabel( FieldList.CURRENT_LIABILITES );
		}
		if (requiredFields.contains( FieldList.CASH ))
		{
			cashField  = addNewEditTextWithLabel( FieldList.CASH );
		}
		if (requiredFields.contains( FieldList.SHORT_TERM_INVESTMENTS ))
		{
			shortTermInvestmentsField = addNewEditTextWithLabel( FieldList.SHORT_TERM_INVESTMENTS );
		}
		if (requiredFields.contains( FieldList.ACCOUNTS_RECIEVABLE ))
		{
			accountsReceivableField = addNewEditTextWithLabel( FieldList.ACCOUNTS_RECIEVABLE );
		}
		if (requiredFields.contains( FieldList.TOTAL_DEBT ))
		{
			totalDebtField = addNewEditTextWithLabel( FieldList.TOTAL_DEBT );
		}
		if (requiredFields.contains( FieldList.TOTAL_ASSETS ))
		{
			totalAssetsField = addNewEditTextWithLabel( FieldList.TOTAL_ASSETS );
		}
		if (requiredFields.contains( FieldList.TOTAL_EQUITY ))
		{
			totalEquityField = addNewEditTextWithLabel( FieldList.TOTAL_EQUITY );
		}
		if (requiredFields.contains( FieldList.INCOME_BEFORE_INCOME_TAXES_AND_INTEREST ))
		{
			incomeBeforeTaxesField = addNewEditTextWithLabel( FieldList.INCOME_BEFORE_INCOME_TAXES_AND_INTEREST );
		}
		if (requiredFields.contains( FieldList.INTEREST_CHARGES ))
		{
			interestChargesField = addNewEditTextWithLabel( FieldList.INTEREST_CHARGES );
		}
		if (requiredFields.contains( FieldList.NET_CREDIT_SALES ))
		{
			netCreditSalesField = addNewEditTextWithLabel( FieldList.NET_CREDIT_SALES );
		}
		if (requiredFields.contains( FieldList.AVERAGE_NET_ACCOUNTS_RECEIVABLE ))
		{
			averageNetAccountsReceivablesField = addNewEditTextWithLabel( FieldList.AVERAGE_NET_ACCOUNTS_RECEIVABLE );
		}
		if (requiredFields.contains( FieldList.COST_OF_GOODS_SOLD ))
		{
			costOfGoodsSoldField = addNewEditTextWithLabel( FieldList.COST_OF_GOODS_SOLD );
		}
		if (requiredFields.contains( FieldList.AVERAGE_INVENTORY ))
		{
			averageInventoryField = addNewEditTextWithLabel( FieldList.AVERAGE_INVENTORY );
		}
		if (requiredFields.contains( FieldList.NET_INCOME ))
		{
			netIncomeField = addNewEditTextWithLabel( FieldList.NET_INCOME );
		}
		if (requiredFields.contains( FieldList.NET_SALES ))
		{
			netSalesField = addNewEditTextWithLabel( FieldList.NET_SALES );
		}
		if (requiredFields.contains( FieldList.GROSS_PROFIT ))
		{
			grossProfitField = addNewEditTextWithLabel( FieldList.GROSS_PROFIT );
		}
		if (requiredFields.contains( FieldList.INTEREST_EXPENSE ))
		{
			interestExpenseField = addNewEditTextWithLabel( FieldList.INTEREST_EXPENSE );
		}
		if (requiredFields.contains( FieldList.AVERAGE_ASSETS ))
		{
			averageAssetsField = addNewEditTextWithLabel( FieldList.AVERAGE_ASSETS );
		}
		if (requiredFields.contains( FieldList.PREFERRED_DIVIDENDS ))
		{
			preferredDividendsField = addNewEditTextWithLabel( FieldList.PREFERRED_DIVIDENDS );
		}
		if (requiredFields.contains( FieldList.AVERAGE_COMMON_EQUITY ))
		{
			averageCommonEquityField = addNewEditTextWithLabel( FieldList.AVERAGE_COMMON_EQUITY );
		}
		if (requiredFields.contains( FieldList.INCOME_AVAILABLE_TO_COMMON ))
		{
			incomeAvailableToCommonField = addNewEditTextWithLabel( FieldList.INCOME_AVAILABLE_TO_COMMON );
		}
		if (requiredFields.contains( FieldList.WEIGHTED_AVERAGE_NUMBER_OF_COMMON_SHARES ))
		{
			weightedAverageNumberOfCommonSharesField = addNewEditTextWithLabel( FieldList.WEIGHTED_AVERAGE_NUMBER_OF_COMMON_SHARES );
		}

	}



	public List<String> getRequiredFieldsList( List<Equation> equationList )
	{
		List<String> requiredFields = new ArrayList<String>();
		List<String> equationRequiredFields;

		for( Equation equation: equationList )
		{
			if( equation == null )
				continue;

			equationRequiredFields = equation.getRequiredFields();

			for( String field: equationRequiredFields )
			{
				if( !requiredFields.contains(field) )
				{
					requiredFields.add( field );
				}
			}
		}
		return requiredFields;
	}



	private EditText addNewEditTextWithLabel( String name )
	{
		TextView label = new TextView( this );
		label.setText( name );
		label.setTextSize(20);
		container.addView( label );

		EditText et = new EditText( this );
		et.setSingleLine( true );
		et.setInputType( InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL );
		et.setTextSize(25);
		container.addView(et);

		return et;
	}
	
	private Map<String, Double> createFieldNameValueMap()
	{
		Map<String, Double> fieldNameValueMap = new Hashtable<String, Double>();
		
		fieldNameValueMap.put( FieldList.CURRENT_ASSETS, currentAssets );
		fieldNameValueMap.put( FieldList.CURRENT_LIABILITES, currentLiabilities );
		fieldNameValueMap.put( FieldList.CASH, cash );
		fieldNameValueMap.put( FieldList.SHORT_TERM_INVESTMENTS, short_term_investments );
		fieldNameValueMap.put( FieldList.ACCOUNTS_RECIEVABLE, accounts_receivable );
		fieldNameValueMap.put( FieldList.TOTAL_DEBT, total_debt );
		fieldNameValueMap.put( FieldList.TOTAL_ASSETS, total_assets );
		fieldNameValueMap.put( FieldList.TOTAL_EQUITY, total_equity );
		fieldNameValueMap.put( FieldList.INCOME_BEFORE_INCOME_TAXES_AND_INTEREST, income_before_taxes );
		fieldNameValueMap.put( FieldList.INTEREST_CHARGES, interest_charges );
		fieldNameValueMap.put( FieldList.NET_CREDIT_SALES, net_credit_sales );
		fieldNameValueMap.put( FieldList.AVERAGE_NET_ACCOUNTS_RECEIVABLE, average_net_accounts_receivable );
		fieldNameValueMap.put( FieldList.COST_OF_GOODS_SOLD, cost_of_goods_sold );
		fieldNameValueMap.put( FieldList.AVERAGE_INVENTORY, average_inventory );
		fieldNameValueMap.put( FieldList.NET_INCOME, net_income );
		fieldNameValueMap.put( FieldList.NET_SALES, net_sales );
		fieldNameValueMap.put( FieldList.GROSS_PROFIT, gross_profit );
		fieldNameValueMap.put( FieldList.INTEREST_EXPENSE, interest_expense );
		fieldNameValueMap.put( FieldList.AVERAGE_ASSETS, average_assets );
		fieldNameValueMap.put( FieldList.PREFERRED_DIVIDENDS, preferred_dividends );
		fieldNameValueMap.put( FieldList.AVERAGE_COMMON_EQUITY, average_common_equity );
		fieldNameValueMap.put( FieldList.INCOME_AVAILABLE_TO_COMMON, income_available_to_common );
		fieldNameValueMap.put( FieldList.WEIGHTED_AVERAGE_NUMBER_OF_COMMON_SHARES, weighted_average_number_of_common_shares );
		
		return fieldNameValueMap;
	}


}
